import { ck_ascii } from 'nui';
import { loc, is_empty } from 'sui';

const ck_label_plc = (v, ckdes, k, data, plcid, cur_plc) => {
    const e = ck_ascii(v, ckdes[k]);
    if (e) {
        return e;
    }

    if (!is_empty(data)) {
        const f = data.find(d => d[k] === v);
        if (f) {
            return loc("cmd_84");
        }
    }

    return null;
};

const ck_k = (t, data, plc) => {
    if (plc === "s7") {
        const s = ["DB", "M", "I", "Q", "C", "T"];
        let f = false;
        s.forEach(d => {
            const pos = data.indexOf(d);
            if (pos >= 0) {
                f = true;
            }
        });

        if (!f) {
            return loc("gscw");
        }

        const tmp = data.split(".");
        if (tmp.length < 2) {
            return loc("gscw");
        }

        if (isNaN(parseInt(tmp[1], 10))) {
            return loc("gscw");
        }

        if (t === "arraybool") {
            if (tmp.length !== 3) {
                return loc("gscw");
            }

            if (isNaN(parseInt(tmp[2], 10))) {
                return loc("gscw");
            }
        }
    }

    return null;
};

const no_array = (t, plc) => {
    if (!t) {
        return true;
    }

    if (t === "bool" || t === "arraybool") {
        return true;
    }

    if (plc === "omron") {
        if (t === "string") {
            return true;
        }
    }

    return false;
};

export {ck_label_plc, ck_k, no_array};
